/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Almacenamiento;

import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 *
 * @author AMD
 */
public class Demo {
    public static void main(String args[]) {
        try {
            List<Atributo> attrs = new ArrayList<Atributo>();
            attrs.add(new Atributo("character", Character.class));
            attrs.add(new Atributo("byte", Byte.class));
            attrs.add(new Atributo("short", Short.class));
            attrs.add(new Atributo("integer", Integer.class));
            attrs.add(new Atributo("long", Long.class));
            attrs.add(new Atributo("float", Float.class));
            attrs.add(new Atributo("double", Double.class));
            attrs.add(new Atributo("string", String.class));
            Relacion relacion = new Relacion(attrs);

            //para agregar una tuplas
            Random r = new  Random();
            int n = r.nextInt(100);
            Tupla t;
            GestorIOTupla man = new GestorIOTupla(relacion, "redes.txt");

            RandomAccessFile raf = new RandomAccessFile("redes.txt", "rw");
            byte[] bytes = new byte[15000];

            int k =0;
            int l =0;
            int [] des= new int[n];
            

            
            for (int i = 0; i < n; i++) {
                List<Comparable> v = new ArrayList<Comparable>();
                v.add(new Character('a'));
                v.add(new Byte((byte) 26));
                v.add(new Short((short) 312));
                v.add(new Integer(2048));
                v.add(new Long(34567));
                v.add(new Float(12.3));
                v.add(new Double(25.6));
                v.add(new String("bla bla"));
                t = new Tupla(new IdentificadorTupla("redes.txt", l), v);
                System.out.println("writing tuple..." + t);
                //man.escribirTupla(t, raf);
                k = man.escribirTupla(t, bytes, k);
                l++;
                des[i] = k;
            }
             raf.close();


             
             raf = new java.io.RandomAccessFile("redes.txt", "r");
            System.out.println("reading tuple...");
            // t = man.leerTupla(bytes);
            
            Integer y=new Integer(0);
            int g = 0;
            
            for (int i = 0; i < n; i++) {
                Par<Tupla, Integer> st = man.leerTupla(bytes, des[g]);
                System.out.println(st.toString());
              //y = st.segundo.intValue();
              System.out.println("");
              g++;            
    
               }
           //man.toString();
            raf.close();

 
            Scanner sr = new Scanner(System.in);
            
            
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
            e.printStackTrace(System.err);
        }
    } // main()
}
